layui.use(function () {
    var $ = layui.jquery;
    var table = layui.table;

    layui.formData = function () {
        var d = {};
        var t = $('form').serializeArray();
        $.each(t, function () {
            d[this.name] = this.value;
        });
        return d;
    };

    layui.dataTable = function (options) {
        options.elem = options.elem == undefined ? '#datatable' : options.elem;
        options.method = options.method == undefined ? 'post' : options.method;
        options.toolbar = options.toolbar == undefined ? '#toolbar' : options.toolbar;
        options.page = options.page == undefined ? true : options.page;
        options.height = options.height == undefined ? 'full-' + (43 + $('.table-search-fieldset').outerHeight()) : options.height;
        let t = table.render({
            elem: options.elem,
            method: options.method,
            url: options.url,
            //contentType: 'application/json',
            where: layui.formData(),
            cols: options.cols,
            even: true,
            request: {
                pageName: 'pageIndex', //页码的参数名称，默认：page
                limitName: 'pageSize', //每页数据量的参数名，默认：limit
            },
            toolbar: options.toolbar,
            parseData: function (res) {
                console.log(res)
                return {
                    code: res.Code == 200 ? 0 : res.Code,
                    msg: res.Message,
                    count: options.page ? res.Count : res.Data.length,
                    data: res.Data,
                };
            },
            height: options.height,
            limits: [10, 15, 20, 25, 50, 100],
            limit: 15,
            page: options.page,
        });
        return t;
    };

    layui.tableReload = function () {
        table.reload('datatable', {
            where: layui.formData(),
            page: { curr: 1 },
        });
    };

    $('#btnQuery').click(function () {
        layui.tableReload();
    });
});
